home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Games / JST / sources / OSEmu / rawdofmt.s < prev    next >
Encoding:
Text File  |  2001-03-19  |  2.1 KB  |  155 lines

  1. ; by Jeff
  2.  
  3. ; RawDoFmt
  4. ; < A0: format string
  5. ; < A1: datastream
  6. ; < A2: PutChProc
  7. ; < A3: PutChData
  8.  
  9. GETCHAR:MACRO
  10.     move.b    (A0)+,D0
  11.     beq.b    .storechar
  12.     ENDM
  13.     
  14.  
  15. RAWDOFMT:
  16. .loop
  17.     GETCHAR
  18.     cmp.b    #'%',D0
  19.     bne.b    .storechar
  20.  
  21.     ; special %
  22.  
  23.     GETCHAR
  24.  
  25.     cmp.b    #'s',D0
  26.     bne.b    .nos
  27.  
  28.     move.l    (A1)+,D1    ; string value
  29.  
  30.     bsr    .strcpy
  31.     bra.b    .loop
  32. .nos
  33.     ; numeric arg
  34.  
  35.     cmp.b    #'l',D0
  36.     bne.b    .nolong
  37.     move.l    (A1)+,D1    ; longword value
  38.     GETCHAR            ; next argument
  39.     bra.b    .nextnum
  40. .nolong
  41.     moveq.l    #0,D1
  42.     move.w    (A1)+,D1    ; word value
  43. .nextnum
  44.     cmp.b    #'d',D0
  45.     bne.b    .nod
  46.  
  47.     move.l    D1,D0    ; value
  48.  
  49.     lea    -20(A7),A7    ; allocates stack
  50.  
  51.     move.l    A7,A1        ; A1: buffer
  52.  
  53.     bsr    _HexToDecString
  54.  
  55.     move.l    A7,D1
  56.     bsr    .strcpy
  57.  
  58.     lea    20(A7),A7    ; deallocates stack
  59.     bra.b    .loop
  60. .nod
  61.     cmp.b    '%',D0
  62.     beq.b    .storechar    ; escaped %% char
  63.  
  64.     ; ERROR, not supported
  65.  
  66.     EMUFAIL    _LVORawDoFmt,_execname
  67.  
  68. .storechar:
  69.     move.l    D0,-(A7)
  70.     jsr    (a2)
  71.     move.l    (A7)+,D0
  72.     tst.b    D0
  73.     beq.b    .out
  74.     bra.b    .loop
  75. .out
  76.     rts
  77.  
  78. .strcpy:
  79.     move.l    A4,-(a7)
  80.  
  81.     move.l    D1,A4
  82. .strloop:
  83.     move.b    (A4)+,D0
  84.     beq.b    .strout
  85.     jsr    (A2)
  86.     bra.b    .strloop
  87. .strout:
  88.     move.l    (A7)+,A4
  89.     rts
  90.  
  91.  
  92. ; *** "ripped" from JST utils
  93. ; *** Converts a hex number to a ascii string of the decimal value
  94. ; < D0: number (-655350:655350), if overflow, Nan or -Nan is returned
  95. ; < A1: pointer to destination buffer (must be at least of size 8)
  96. ; out: nothing
  97.  
  98. _HexToDecString:
  99.     movem.l    D0-A6,-(A7)
  100.  
  101.     tst.l    D0
  102.     bpl.b    .positive
  103.     neg.l    D0    ; D0 = -D0
  104.     move.b    #'-',(A1)+
  105. .positive
  106.  
  107.     cmp.l    #655351,D0
  108.     bcs.b    .ok
  109.  
  110.     ; overflow
  111.  
  112.     move.l    A1,A3
  113.     move.b    #'N',(A3)+
  114.     move.b    #'a',(A3)+
  115.     move.b    #'N',(A3)+
  116.     bra    .end
  117. .ok
  118.     move.l    A1,A2    ; store user buffer pointer
  119.  
  120.     move.l    D0,D2
  121.     moveq.l    #0,D3
  122.  
  123. .loop
  124.     divu    #10,D2
  125.     swap    D2    
  126.     move.w    D2,D3    ; D3=remainder
  127.  
  128.     add.b    #'0',D3
  129.     move.b    D3,(A2)+    ; store the number in reverse
  130.  
  131.     clr.w    D2
  132.     swap    D2    ; D2=result
  133.     tst.l    D2
  134.     bne.b    .loop
  135.  
  136.     ; division over, now reverse the number in the buffer
  137.  
  138.     move.l    A2,A3    ; store end of string
  139.  
  140.     move.l    A2,D0
  141.     sub.l    A1,D0    ; D0: number of digits
  142.     lsr    #1,D0    ; only swap on half!
  143.     beq.b    .end    ; 1 char: no swap
  144.     subq.l    #1,D0
  145. .reverse
  146.     move.b    -(A2),D2
  147.     move.b    (A1),(A2)
  148.     move.b    D2,(A1)+
  149.     dbf    D0,.reverse
  150. .end
  151.     clr.b    (A3)
  152.  
  153.     movem.l    (A7)+,D0-A6
  154.     rts
  155.